package com.ebt.m.utils;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.ebt.m.R;

import java.lang.reflect.Field;
import java.util.Calendar;


public class UIHelper {
    /**
     * 网络不可用
     */
    public static final int EXCEPTION_INTERNET_FAILED = 0;
    /**
     * 网络可用,但仅使用wifi
     */
    public static final int EXCEPTION_INTERNET_ONLY_WIFI = 1;

    /**
     * 无资源要下载
     */
    public static final int EXCEPTION_NO_RES_DOWNLOAD = 3;
    /**
     * 下载失败
     */
    public static final int EXCEPTION_DOWNLOAD_FALSE = 4;
    /**
     * 登录密码错误
     */
    public static final int ALERT_PWD_ERROR = 2;
    /**
     * 网络获取异常 MSG
     */
    public static final int EXCEPTION_EXCEPTION_CONNECT_FALSE = 5;
    /**
     * 网络连接超时
     */
    public static final int EXCEPTION_CONNECT_TIMEOUT = 6;
    /**
     * 无需下载标志 MSG
     */
    public static final int EXCEPTION_DOWNLOAD_NOT_UPDATE = 8;
    /**
     * 更新失败 MSG
     */
    public static final int EXCEPTION_UPDATE_DATA_FALSE = 10;
    /**
     * 检查更新失败 MSG
     */
    public static final int EXCEPTION_CHECK_UPDATE_DATA_FALSE = 11;
    /**
     * 已经是最新数据 不需要更新 MSG
     */
    public static final int EXCEPTION_NOT_NEED_UPDATE_DATA = 12;
    /**
     * 发送信息失败
     */
    public static final int EXCEPTION_SEND_DATA_FALSE = 13;
    /**
     * 已经在下载中 MSG
     */
    public static final int EXCEPTION_DOWNLOAD_IS_DOWNLOADING = 14;
    /**
     * 操作失败，请重试
     */
    public static final int ALERT_OPT_FAILED = 15;


    public static void flipPrevious(Context context, ViewFlipper flipper) {
        flipper.setInAnimation(context, R.anim.flipper_left_in);
        flipper.setOutAnimation(context, R.anim.flipper_right_out);
        flipper.showPrevious();
    }

    public static void flipNext(Context context, ViewFlipper flipper) {
        flipper.setInAnimation(context, R.anim.flipper_right_in);
        flipper.setOutAnimation(context, R.anim.flipper_left_out);
        flipper.showNext();
    }

    /**
     * Toast提示消息
     *
     * @param context
     * @param text
     * @param isShort LENGTH_SHORT
     */
    public static void makeToast(Context context, CharSequence text, boolean isShort) {
        int duration = isShort ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG;
        Toast.makeText(context, text, duration).show();
    }

    public static void makeToast(Context context, CharSequence text) {
        if (context != null) {
            Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * Toast提示消息
     *
     * @param context
     * @param resId
     * @param isShort LENGTH_SHORT
     */
    public static void makeToast(Context context, int resId, boolean isShort) {
        int duration = isShort ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG;
        Toast.makeText(context, resId, duration).show();
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return (int) (dpValue * metrics.density + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return (int) (pxValue / metrics.density + 0.5f);
    }

    /**
     * 单位是dp
     *
     * @param context
     * @param button
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    public static void setPadding(Context context, Button button, float left, float top, float right, float bottom) {
        button.setPadding(button.getPaddingLeft() + dip2px(context, left), button.getPaddingTop() + dip2px(context, top), button.getPaddingRight() + dip2px(context, right), button.getPaddingBottom()
                + dip2px(context, bottom));
    }

    public static void setPadding(Context context, EditText textView, float left, float top, float right, float bottom) {
        textView.setPadding(dip2px(context, left), dip2px(context, top), dip2px(context, right), dip2px(context, bottom));
    }

    /**
     * 隐藏输入法
     *
     * @param activity
     */
    public static void hideSoftInputFromWindow(Activity activity) {
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

    /**
     * 隐藏输入法
     *
     * @param view
     */
    public static void hideSoftInputFromWindow(View view) {
        InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

    /**
     * 显示键盘
     *
     * @param context
     * @param view
     */
    public static void showInputMethodFromView(Context context, View view) {
        InputMethodManager im = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        im.showSoftInput(view, 0);
    }

    /**
     * 设置文本选中颜色 选中时为白色 反正为黑色
     *
     * @param selected 是否选中
     * @param views
     */
    public static void setTextColor(boolean selected, TextView... views) {
        for (int i = 0; i < views.length; i++) {
            views[i].setTextColor(selected ? Color.WHITE : Color.BLACK);
        }
    }

    /**
     * 设置列表项选中
     *
     * @param selected
     * @param parent
     * @param views
     */
    public static void setListItemSelected(boolean selected, View parent, TextView... views) {
        if (selected) {
            parent.setBackgroundResource(R.color.list_item_focused);
        } else {
            parent.setBackgroundResource(R.color.full_transparent);
        }
        setTextColor(selected, views);
    }

    /**
     * 设置表格选中的背景色 选中时为绿色 反正为白色
     *
     * @param context
     * @param selected 是否选中
     * @param views
     */
    public static void setBackgroundColor(Context context, boolean selected, TextView... views) {
        int color = Color.WHITE;
        if (selected) {
            color = context.getResources().getColor(R.color.list_item_focused);
        }
        for (int i = 0; i < views.length; i++) {
            views[i].setBackgroundColor(color);
        }
    }

    public static void keepDialog(DialogInterface dialog) {
        try {
            Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
            field.setAccessible(true);
            field.set(dialog, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void distoryDialog(DialogInterface dialog) {
        try {
            Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
            field.setAccessible(true);
            field.set(dialog, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void openExplorer(Context context, String uriString) {
        Uri uri = Uri.parse(uriString);
        context.startActivity(new Intent(Intent.ACTION_VIEW, uri));
    }


    public static String getAlertMsg(int msgUICode, Context ctx) {
        String msg = "";
        if (msgUICode == EXCEPTION_INTERNET_FAILED) {

            msg = ctx.getResources().getString(R.string.network_fail);
        } else if (msgUICode == EXCEPTION_INTERNET_ONLY_WIFI) {

            msg = ctx.getResources().getString(R.string.alert_network_set);
        } else if (msgUICode == EXCEPTION_NO_RES_DOWNLOAD) {

            msg = ctx.getResources().getString(R.string.alert_no_resource_download);
        } else if (msgUICode == EXCEPTION_DOWNLOAD_FALSE) {

            msg = ctx.getResources().getString(R.string.alert_download_false);
        } else if (msgUICode == EXCEPTION_EXCEPTION_CONNECT_FALSE) {

            msg = ctx.getResources().getString(R.string.alert_network);
        } else if (msgUICode == EXCEPTION_CONNECT_TIMEOUT) {

            msg = ctx.getResources().getString(R.string.network_fail);
        } else if (msgUICode == EXCEPTION_DOWNLOAD_NOT_UPDATE) {

            msg = ctx.getResources().getString(R.string.alert_download_not_update);
        } else if (msgUICode == EXCEPTION_UPDATE_DATA_FALSE) {

            msg = ctx.getResources().getString(R.string.alert_update_data_false);
        } else if (msgUICode == EXCEPTION_CHECK_UPDATE_DATA_FALSE) {

            msg = ctx.getResources().getString(R.string.alert_check_update_data_false);
        } else if (msgUICode == EXCEPTION_NOT_NEED_UPDATE_DATA) {

            msg = ctx.getResources().getString(R.string.alert_not_need_upadate_data_false);
        } else if (msgUICode == EXCEPTION_SEND_DATA_FALSE) {

            msg = ctx.getResources().getString(R.string.alert_send_data_false);
        } else if (msgUICode == EXCEPTION_DOWNLOAD_IS_DOWNLOADING) {

            msg = ctx.getResources().getString(R.string.alert_download_is_downloading);
        } else if (msgUICode == ALERT_OPT_FAILED) {

            msg = ctx.getResources().getString(R.string.alert_sys_card_failed);
        } else if (msgUICode == ALERT_PWD_ERROR) {

            msg = ctx.getResources().getString(R.string.alert_pwd_error);
        }
        return msg;
    }

    private static long lastClickTime;

    /**
     * 是否快速点击
     *
     * @return
     */
    public static boolean isFastDoubleClick() {
        long curTime = System.currentTimeMillis();
        long timeD = curTime - lastClickTime;
        if (0 < timeD && timeD < 1000) {
            return true;
        }
        lastClickTime = curTime;
        return false;
    }

    /**
     * custom recycle view onItemClickListener
     */
    public static abstract class CustomNoDoubleOnItemClickListener {

        public static final int MIN_CLICK_DELAY_TIME = 1000;
        private long lastClickTime = 0;

        public void onItemClick(View view, int position) {
            long currentTime = Calendar.getInstance().getTimeInMillis();
            if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) {
                lastClickTime = currentTime;
                onNoDoubleItemClick(view, position);
            }
        }

        public abstract void onNoDoubleItemClick(View view, int position);
    }
}
